<template>
  <el-select
    class="dg-service-select"
    :data-type="dataType"
    :multiple="multiple"
    :collapse-tags="collapseTags"
    :value-key="valueKey"
    :allow-create="allowCreate"
    :filterable="filterable"
    :remote="remote"
    :remote-method="remoteMethod"
    :clearable="clearable"
    :size="size"
    :placeholder="placeholder"
    :loading="loading"
    :disabled="disabled"
    v-on="$listeners"
    v-model="checkedVal">
    <el-option
      v-for="(item, index) in optionsData"
      :key="`${item.valueShow}-${index}`"
      :label="item.labelShow"
      :value="isObject !== undefined ? item : item.valueShow">
    </el-option>
  </el-select>
</template>

<script>
import * as API_Select from './api';

export default {
  name: 'DgServiceSelect',
  model: {
    event: 'change'
  },
  /**
    * dataType: 数据来源类型;
    * serviceParams: 搜索条件;
    * placeholder: 占位符;
    * multiple: 多选;
    * collapseTags: 多选时是否将选中值按文字的形式展示;
    * valueKey: value 唯一标识的键名，绑定值为对象类型时必填;
    * isObject: 返回值需要的类型是否为对象
    * filterable: 筛选
    * remote: 远程搜索;
    * clearable: 清空;
    * disabled: 禁用;
    * dictType: 字典类型;
    * defaultData: 默认值
    * needOpen: 司机、车辆、承运商、物流公司 启用状态
  */
  props: ['value', 'dataType', 'paramKey', 'labelKey', 'serviceParams', 'allowCreate', 'placeholder', 'multiple', 'collapseTags', 'isObject', 'filterable', 'remote', 'clearable', 'size', 'disabled', 'dictType', 'needOpen'],
  data () {
    return {
      valueKey: undefined,
      checkedVal: this.value,
      loading: false,
      optionsData: []
    };
  },
  watch: {
    /**
      * @date 2025/07/05
      * @author wolfMan
      * @description {监听参数变化}
    */
    serviceParams (_newVal, _oldVal) {
      if (JSON.stringify(_newVal) === JSON.stringify(_oldVal)) { return; };
      this.remoteMethod();
    },
    /**
      * @date 2025/07/05
      * @author wolfMan
      * @description {监听值变化}
    */
    value (newVal) {
      this.$nextTick(() => {
        if (this.remote !== undefined && this.isObject !== undefined) {
          let _optionsData = this.multiple !== undefined && this.multiple ? newVal : !newVal || (!newVal.valueShow && !newVal.labelShow) ? [] : [newVal];
          this.optionsData = _optionsData;
        };
      });
      this.checkedVal = this.isObject === undefined ? newVal : newVal || {};
    },
    /**
      * @date 2025/07/05
      * @author wolfMan
      * @description {监听选中值变化}
    */
    checkedVal (newVal) {
      this.$emit('change', newVal);
    }
  },
  created () {
    if (this.isObject !== undefined) {
      this.valueKey = 'valueShow';
    };
    // 初始设置默认选项
    let _value = this.value;
    if (_value && (_value.valueShow || _value.valueShow === 0) && this.remote !== undefined && this.isObject !== undefined) {
      let _optionsData = this.multiple === undefined || this.multiple === false ? [_value] : _value;
      this.optionsData = _optionsData;
    };
  },
  mounted () {
    this.needValue = this.value;
    // 非远程搜索
    if ((this.remote === false || this.remote === undefined) && this.dataType) {
      this.remoteMethod();
    };
  },
  methods: {
    /**
      * @date 2025/07/05
      * @author wolfMan
      * @description {获取数据源}
    */
    remoteMethod (_content) {
      // 远程搜索且未输入
      if (this.remote !== undefined && !_content) {
        return;
      };
      this.loading = true;
      let _service;
      // 基础模块:100开头; archives模块: 200开头
      switch (this.dataType) {
        case 10000: // 字典类型
          _service = API_Select.getDictOptionList;
          break;
        case 20000: // 客户列表
          _service = API_Select.getCustomerOptionList;
          break;
        case 20001: // 分区国家
          _service = API_Select.getCountryOptionList;
          break;
        case 20002: // 运输产品
          _service = API_Select.getProductOptionList;
          break;
        case 20003: // 发件人
          _service = API_Select.getShipperOptionList;
          break;
        case 20004: // 收件人
          _service = API_Select.getConsigneeOptionList;
          break;
        default:
          break;
      };
      let _params = {
        pageNum: 1,
        pageSize: 999
      };
      if (this.serviceParams) {
        _params = {
          ..._params,
          ...this.serviceParams
        };
      };
      if (this.remote !== undefined && this.remote !== false) {
        if (this.paramKey) {
          _params[this.paramKey] = _content;
        } else {
          switch (this.dataType) {
            case 20000: // 客户列表
              _params.cusName = _content;
              break;
            case 20001: // 分区国家
              _params.countryName = _content;
              break;
            case 20002: // 运输产品
              _params.prodName = _content;
              break;
            case 20003: // 发件人
              _params.shipperName = _content;
              break;
            case 20004: // 收件人
              _params.consigneeName = _content;
              break;
            default:
              break;
          };
        };
        _params.pageSize = 10;
      };
      let _data = [];
      _service(_params).then(response => {
        let _label = '';
        let _valueKey = 'id';
        _data = response.rows || [];
        switch (this.dataType) {
          case 10000: // 字典类型
            _data = response.data || [];
            _label = 'dictName';
            _valueKey = 'dictType';
            break;
          case 20000: // 客户列表
            _label = 'cusName';
            break;
          case 20001: // 分区国家
            _label = 'countryName';
            break;
          case 20002: // 运输产品
            _label = 'prodName';
            break;
          case 20003: // 发件人
            _label = 'shipperName';
            break;
          case 20004: // 收件人
            _label = 'consigneeName';
            break;
          default:
            break;
        };
        _data = _data.map(item => {
          item.labelShow = this.labelKey ? item[this.labelKey] : item[_label];
          item.valueShow = item[_valueKey];
          return item;
        });
        this.$nextTick(() => {
          this.optionsData = _data;
        });
      }).always(() => {
        this.loading = false;
      });
    }
  }
};
</script>

<style type="text/scss" lang="scss">
.dg-select {
  width: 100%;
}
</style>